/*
 * This file is part of aion-engine <aion-engine.net>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.services.json;

import com.aionengine.gameserver.cache.HTMLCache;
import com.aionengine.gameserver.configs.main.GSConfig;
import com.aionengine.gameserver.configs.main.JsonConfig;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.services.HTMLService;
import com.aionengine.gameserver.services.MuiService;
import com.aionengine.gameserver.utils.api.vk.JsonVkLoader;
import com.aionengine.gameserver.utils.api.vk.JsonVkLoader.VkItem;
import javolution.util.FastList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Dr2co
 */

public class JsonService {

    private static final Logger log = LoggerFactory.getLogger(JsonService.class);
    private FastList<VkItem> vkItemList = FastList.newInstance();

    public void load() {
        if (!JsonConfig.ENABLE_JSON) {
            log.info("JSON Service disable...");
            return;
        }

        if (JsonConfig.ENABLE_VK_JSON) {
            vkItemList = JsonVkLoader.load(JsonConfig.VK_GROUP_ID, JsonConfig.VK_GROUP_ITEMS_COUNT);
            log.info("JSON Service loaded " + vkItemList.size() + " vk items");
        }
    }

    public void onPlayerLogin(Player player) {
        if (!JsonConfig.ENABLE_JSON) {
            return;
        }
        String context = HTMLCache.getInstance().getHTML("apinews.xhtml");
        context = context.replace("%title%", MuiService.getInstance().getMessage("JSON_TITLE", GSConfig.SERVER_NAME));

        StringBuilder sb = new StringBuilder();
        for (FastList.Node<VkItem> n = vkItemList.head(), end = vkItemList.tail(); (n = n.getNext()) != end; ) {
            String title;
            if (n.getValue().getText().length() == 0) {
                title = n.getValue().getName();
            } else {
                try {
                    title = n.getValue().getText().substring(0, 90) + "...";
                } catch (Exception ex) {
                    title = n.getValue().getText();
                }
            }
            sb.append("<a href='http://vk.com/").append(n.getValue().getGroupName()).append("?w=wall-").append(JsonConfig.VK_GROUP_ID).append("_").append(n.getValue().getId()).append("'><font color='45688E'>").append(MuiService.getInstance().convertFromUTF8(title)).append("</font></a><br>");
            sb.append(MuiService.getInstance().convertFromUTF8(n.getValue().getText())).append("<br>");
            sb.append(MuiService.getInstance().getMessage("VK_COMMENTS_LIKE", n.getValue().getTimeFormat(), n.getValue().getCommnets(), n.getValue().getLikes())).append("<br><br><br><br>");
        }

        context = context.replace("%text%", sb);

        HTMLService.showHTML(player, context);
    }

    private static class SingletonHolder {

        protected static final JsonService instance = new JsonService();
    }

    public static final JsonService getInstance() {
        return SingletonHolder.instance;
    }
}
